﻿using OrderSystem.Common;
using OrderSystem.SystemFramework;
using OrderSystem.Templet;
using System;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Windows.Forms;

namespace OrderSystem
{
    public partial class FormDeleteData : FormTemplet
    {
        public FormDeleteData()
        {
            InitializeComponent();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (LoginInfo.Role.RoleID != 0)
            {
                MessageBox.Show("只有管理员才能执行这个操作", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else if (MessageBox.Show("警告：此操作所删除的所有数据将不可恢复\r\n\r\n你是否要继续这个操作？", "提示！", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) != DialogResult.No)
            {
                string cmdText = "SET XACT_ABORT ON BEGIN TRANSACTION delete ShatterItem from ShatterItem as si inner join Shatter as s on si.ShatterID=s.PKID where s.[Date]>=CONVERT(DATETIME,@dataFrom) and s.[Date]<=CONVERT(DATETIME,@dataTo) delete Shatter where [Date]>=CONVERT(DATETIME,@dataFrom) and [Date]<=CONVERT(DATETIME,@dataTo) delete InvoiceItem from InvoiceItem as ii inner join Invoice as i on ii.InvoiceID=i.PKID where i.InvoiceDate>=CONVERT(DATETIME,@dataFrom) and i.InvoiceDate<=CONVERT(DATETIME,@dataTo) delete Invoice where InvoiceDate>=CONVERT(DATETIME,@dataFrom) and InvoiceDate<=CONVERT(DATETIME,@dataTo) delete Product from Product as p inner join [Order] as o on p.OrderID=o.PKID where o.SpeakForDate>=CONVERT(DATETIME,@dataFrom) and o.SpeakForDate<=CONVERT(DATETIME,@dataTo) delete [Order] where SpeakForDate>=CONVERT(DATETIME,@dataFrom) and SpeakForDate<=CONVERT(DATETIME,@dataTo) COMMIT TRANSACTION";
                bool flag = false;
                try
                {
                    SqlCommand sqlCommand = new SqlCommand(cmdText, new SqlConnection(AppSettings.ConnectionString));
                    sqlCommand.Connection.Open();
                    sqlCommand.Parameters.Add(new SqlParameter("@dataFrom", SqlDbType.VarChar, 32));
                    sqlCommand.Parameters.Add(new SqlParameter("@dataTo", SqlDbType.VarChar, 32));
                    sqlCommand.Parameters["@dataFrom"].Value = this.dtpFrom.Value.ToShortDateString() + " 00:00:00.000";
                    sqlCommand.Parameters["@dataTo"].Value = this.dtpTo.Value.ToShortDateString() + " 23:59:59.999";
                    sqlCommand.ExecuteNonQuery();
                    flag = true;
                }
                catch (SqlException ex)
                {
                    MessageBox.Show(ex.Message + "\r\n\r\n数据库出现了问题", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                catch (Exception ex2)
                {
                    MessageBox.Show(ex2.Message, "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                if (flag)
                {
                    MessageBox.Show("你所选择时间段的所有数据已经全部删除！", "Success!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                }
            }
        }
    }
}
